package com.ljfiction.book.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ljfiction.book.R;

/**
 * Created by Android on 2017/11/13.
 */

public class ThreeDialog {
    private Dialog mDialog;
    private Listener listener;
    private TextView isOk;
    private TextView ok;
    private TextView close;
    public ThreeDialog(Context context, boolean isSelectClose){
        mDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view= LayoutInflater.from(context).inflate(R.layout.isornodialog, null);
        //将布局设置给Dialog
        mDialog.setContentView(view);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        view.setLayoutParams(layoutParams);
        mDialog.getWindow().setGravity(Gravity.BOTTOM);
        mDialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        mDialog.setCancelable(isSelectClose);
        close = (TextView) mDialog.findViewById(R.id.closethis);
        ok = (TextView) mDialog.findViewById(R.id.isok);
        isOk = (TextView) mDialog.findViewById(R.id.isorok);
        isOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.isOkClick();
                mDialog.dismiss();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                listener.closeClick();
                mDialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener!=null) {
                    listener.okClick();
                    mDialog.dismiss();
                }
            }
        });

    }

    public void setListener(Listener listener,String topStr, String closeStr, String okStr, boolean isSelectClose){
        this.listener=listener;
        if (topStr!=null){
            isOk.setText(topStr);
        }
        if (closeStr!=null) {
            close.setText(closeStr);
        }
        if (okStr!=null) {
            ok.setText(okStr);
        }
        mDialog.setCancelable(isSelectClose);
        show();
    }
    public void show(){
        if (mDialog!=null) {
            mDialog.show();
        }
    }

    public void setListener(Listener listener) {
        this.listener=listener;
        show();
    }


    public interface Listener{
        public void isOkClick();
        public void okClick();
        public void closeClick();

    }
}
